﻿using System;
using Microsoft.Office.Server.Search.WebControls;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebPartPages;

namespace SP2010
{
    public static class Webparts
    {
        #region Helper Methods

        public static CoreResultsWebPart CoreSearchResultsWebPart(string title)
        {
            CoreResultsWebPart crwp = new CoreResultsWebPart();
            crwp.Title = title;
            crwp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
            crwp.SpellcheckingMode = Microsoft.Office.Server.Search.Query.SpellcheckMode.Suggest;
            crwp.AllowConnect = false;
            crwp.HelpMode = System.Web.UI.WebControls.WebParts.WebPartHelpMode.Modeless;
            crwp.CharactersInUrl = 90;
            crwp.ShowLangPicker = true;
            crwp.DisplayName = string.Empty;
            crwp.MoreLinkUrl = null;
            crwp.ShowActionLinks = false;
            crwp.ViewFlag = "0";
            crwp.QueryNumber = QueryId.Query1;
            crwp.PreviewWindowSize = "160";
            crwp.DateFormat = Microsoft.SharePoint.Utilities.SPDateFormat.DateOnly;
            crwp.AllowClose = true;
            crwp.SentencesInSummary = 3;
            crwp.HighestResultPage = 10000;
            crwp.CacheXslTimeOut = 86400;
            crwp.RelevanceView = false;
            crwp.MoreLinkLabel = "View more results";
            crwp.ManualRefresh = false;
            crwp.ListUrl = null;
            crwp.FixedQuery = string.Empty;
            crwp.CacheXslStorage = true;
            crwp.ResubmitFlag = Microsoft.Office.Server.Search.Query.ResubmitFlag.NoResubmit;
            //crwp.XslLink = null;
            crwp.EnableStemming = false;
            crwp.ShowSearchResults = true;
            crwp.AllowMinimize = true;
            crwp.AllowHide = true;
            crwp.ShowMoreLink = true;
            crwp.ListDisplayName = null;
            crwp.EnableDocumentPreviewPowerPoint = true;
            crwp.DefaultRankingModelID = string.Empty;
            crwp.CatalogIconImageUrl = string.Empty;
            crwp.Default = string.Empty;
            crwp.DisplayRSSLink = false;
            crwp.FireInitialRow = true;
            crwp.TotalNumberDocumentPreview = "10";
            crwp.DateView = false;
            crwp.ViewFlags = Microsoft.SharePoint.SPViewFlags.None;
            crwp.DisplayAlertMeLink = false;
            crwp.DataSourceID = string.Empty;
            crwp.HelpUrl = string.Empty;
            crwp.DisplayDiscoveredDefinition = true;
            crwp.AppendedQuery = string.Empty;
            crwp.AsyncRefresh = false;
            crwp.IgnoreNoise = true;
            crwp.Height = string.Empty;
            crwp.PageSize = -1;
            crwp.UseSQLDataSourcePaging = true;
            crwp.AllowEdit = true;
            crwp.ViewContentTypeId = string.Empty;
            crwp.Hidden = false;
            crwp.DefaultSort = ResultsView.Relevance;
            crwp.ExportMode = System.Web.UI.WebControls.WebParts.WebPartExportMode.All;
            crwp.ConcurrentNumberDocumentPreview = "10";
            crwp.ListName = null;
            crwp.PropertiesToRetrieve = "<Columns><Column Name=\"WorkId\"/><Column Name=\"Rank\"/><Column Name=\"Title\"/><Column Name=\"Author\"/><Column Name=\"Size\"/><Column Name=\"Path\"/><Column Name=\"Description\"/><Column Name=\"Write\"/><Column Name=\"SiteName\"/><Column Name=\"CollapsingStatus\"/><Column Name=\"HitHighlightedSummary\"/>	<Column Name=\"HitHighlightedProperties\"/>	<Column Name=\"ContentClass\"/>	<Column Name=\"IsDocument\"/>	<Column Name=\"PictureThumbnailURL\"/>	<Column Name=\"PopularSocialTags\"/>	<Column Name=\"PictureWidth\"/>	<Column Name=\"PictureHeight\"/>	<Column Name=\"DatePictureTaken\"/>	<Column Name=\"ServerRedirectedURL\"/></Columns>";
            crwp.ParameterBindings = null;
            crwp.DataSourcesString = string.Empty;
            crwp.EnablePhonetic = false;
            crwp.ListId = Guid.Empty;
            crwp.Scope = "WHAOL";
            crwp.XmlDefinitionLink = string.Empty;
            crwp.DataFields = string.Empty;
            crwp.ShowMessages = false;
            crwp.Width = string.Empty;
            crwp.InitialAsyncDataFetch = false;
            crwp.QuerySideLanguage = "-1";
            crwp.MoreResultsLink = null;
            crwp.XmlDefinition = string.Empty;
            crwp.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
            crwp.PageType = Microsoft.SharePoint.PAGETYPE.PAGE_NORMALVIEW;
            crwp.ScopeID = 1;
            crwp.SelectColumns = "<Columns><Column Name=\"WorkId\"/><Column Name=\"Rank\"/><Column Name=\"Title\"/><Column Name=\"Author\"/><Column Name=\"Size\"/><Column Name=\"Path\"/><Column Name=\"Description\"/><Column Name=\"Write\"/><Column Name=\"SiteName\"/><Column Name=\"CollapsingStatus\"/><Column Name=\"HitHighlightedSummary\"/>	<Column Name=\"HitHighlightedProperties\"/>	<Column Name=\"ContentClass\"/>	<Column Name=\"IsDocument\"/>	<Column Name=\"PictureThumbnailURL\"/>	<Column Name=\"PopularSocialTags\"/>	<Column Name=\"PictureWidth\"/>	<Column Name=\"PictureHeight\"/>	<Column Name=\"DatePictureTaken\"/>	<Column Name=\"ServerRedirectedURL\"/></Columns>";
            crwp.SampleData = "<All_Results>  <Result>    <workid>1</workid>    <rank>222</rank>    <title>Title of document or web page</title>    <author>Author of document or web page</author>    <size>1025</size>    <sitename>http://www.sample.com</sitename>    <url>http://www.sample.com/folder/document.aspx</url>    <imageurl>/_layouts/images/aspx16.gif</imageurl>    <description>This is the summary of the document or web page. The summary is generated from the original document based on matches with query terms. In some cases, the summary is a description provided by the author.</description>    <write>December 26, 2004</write>  </Result></All_Results>";
            crwp.EnableDocumentPreviewWord = true;
            crwp.AutoRefreshInterval = 60;
            crwp.CharactersInSummary = 185;
            crwp.TitleUrl = string.Empty;
            crwp.RemoveDuplicates = true;
            crwp.Xsl = string.Empty;
            crwp.View = ResultsView.Relevance;
            crwp.TitleIconImageUrl = string.Empty;
            crwp.EnableSimilarFind = false;
            crwp.Direction = System.Web.UI.WebControls.ContentDirection.NotSet;
            crwp.AllowZoneChange = true;
            crwp.ResultsPerPage = 3000;
            crwp.ShowWithSampleData = false;
            crwp.Description = "Displays the search results and the properties associated with them.";
            crwp.EnableNicknames = false;
            crwp.AutoRefresh = false;
            crwp.IsLocationPeople = false;
            crwp.Location = string.Empty;
            crwp.NoDefaultStyle = string.Empty;
            crwp.ShowWindowsSearch = true;
            crwp.UseLocationVisualization = false;
            crwp.MoreResultsText = "View more results";
            crwp.XslLink = "/_layouts/Client/CustomSearchResults.xslt";
            return crwp;
        }

        public static void ProvisionCustomPage(PublishingWeb web,string pageNameNoExtension, string pageTitle)
        {
            // Create the summary info page
            PublishingPageInfo customPageInfo = new PublishingPageInfo()
            {
                Name = pageNameNoExtension + ".aspx",
                Title = pageTitle,
                ContentTypeId = PageLayouts.CustomLayoutContentTypeID,
                IsDefaultPage = false,
                FolderName = "ApplicationName"              
            };


            ContentEditorWebPart cw = SPFoundation.Webparts.GetContentEditorWP("Test Content");
            customPageInfo.WebParts.Add(
                new WebPartInfo()
                {
                    WebPart = cw,
                    Zone = "LeftZone",
                    Order = 1
                });


            CoreResultsWebPart crwp = CoreSearchResultsWebPart("Test Core");
            customPageInfo.WebParts.Add(
                new WebPartInfo()
                {
                    WebPart = crwp,
                    Zone = "ContentMiddleZone",
                    Order = 1
                }
            );

            SiteInfrastructureHelper.CreateCustomPublishingPage(web, customPageInfo);
        }


        #endregion
    }
}
